// *************************************************************************************************
//
// MK3D Engine
// --------------------------------------
// Copyright (C) 2007-2011 Zeb (mailto: zebbey@gmail.com)
//
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// *************************************************************************************************

#ifndef __H_MK_SHADOW_TSM__
#define __H_MK_SHADOW_TSM__

namespace MK3DE
{	
	/**
		Trapezoidal Shadow Mapping
	*/
	class MK3DE_API CShadowTSM
	{
	public:
		CShadowTSM();
		~CShadowTSM();

		void SetFocusPercent(float srcPercent, float dstPercent);

		void CalcTrapezoidalTransformation(const D3DXMATRIX& matViewProj, D3DXVECTOR3& lightDir);

		const D3DXMATRIX& GetMatrixLightViewProj() const { return _matLightViewProj; }
		const D3DXMATRIX& GetMatrixTrapezoidal() const { return _matTrapezoidal; }

		D3DXVECTOR3 _frustum[8];

	private:
		void __TransformFrustumToWorldSpace(const D3DXMATRIX& matViewProj, D3DXVECTOR3* frustumWorld);
		void __CalcMatrixLightViewProj(D3DXVECTOR3& lightDir, D3DXVECTOR3* frustum);
		void __Calc2DConvexCullBorder(D3DXVECTOR3* frustum);
		void __CalcTrapezoidal(D3DXVECTOR3* frustum);

	private:
		D3DXMATRIX _matLightViewProj;
		D3DXMATRIX _matTrapezoidal;

		DWORD _topLeftIdx;
		DWORD _topRightIdx;
		DWORD _bottomLeftIdx;
		DWORD _bottomRightIdx;

		DWORD _topIdx;
		DWORD _bottomIdx;
		DWORD _nearIdx;
		DWORD _farIdx;

		float _srcPercent;
		float _dstPercent;
	};
}

#endif
